package com.yx.learn.service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yx.learn.dao.AuthorityDao;
import com.yx.learn.dao.UserAuthorityDao;
import com.yx.learn.domain.Authority;
import com.yx.learn.domain.UserAuthority;
import com.yx.learn.service.UserAuthorityService;

@Service("userAuthorityService")
@Transactional
public class UserAuthorityServiceImpl implements UserAuthorityService {
	
	@Autowired
	@Qualifier("userAuthorityDao")
	private UserAuthorityDao userAuthorityDao;
	@Autowired
	@Qualifier("authorityDao")    
	private AuthorityDao authorityDao;
	

	public List<Authority> getUserAuthorityByNo(String employNo)
			throws Exception {
		// TODO Auto-generated method stub
		return this.userAuthorityDao.findUserAuthorityByNo(employNo);
	}

	public List<Authority> getUserAuthorityOfMenuByNo(String employNo)
			throws Exception {
		// TODO Auto-generated method stub
		return this.userAuthorityDao.findUserAuthorityOfMenuByNo(employNo);
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<UserAuthority> getUserAuthorities() throws Exception {
		// TODO Auto-generated method stub
		return userAuthorityDao.getUserAuhtorities();
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public UserAuthority findUserAuthority(int uid,int aid) throws Exception {
		// TODO Auto-generated method stub
		return userAuthorityDao.findUserAuthority(uid,aid);
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Integer> getUserAuthorityUID(int uid) throws Exception {
		List<Integer> authorityID = userAuthorityDao.getUserAuthorityUID(uid);
		return authorityID;
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Authority> findAllAuthority(int uid, int id) throws Exception {
		JSONArray ja = new JSONArray();
		List<Authority> list = authorityDao.getAuthoritiesByPid(id);
		List<Integer> authorityID = getUserAuthorityUID(uid);
		for (int i = 0; i < list.size(); i++) {
			Boolean hasChildren = false;
			if (authorityDao.getAuthoritiesByPid(list.get(i).getId()).size() > 0) {
				hasChildren = true;
			}
			JSONObject jo = new JSONObject();
			jo.put("id", list.get(i).getId());
			jo.put("text", list.get(i).getAuthorityName());
			for (int j = 0; j < authorityID.size(); j++) {
				if (list.get(i).getId() == authorityID.get(j)) {
					jo.put("checked", "ture");
					break;
				}
			}			
			if (hasChildren) {
				jo.put("state", "closed");

			} else {
				jo.put("state", "open");
			}
			System.out.println(jo.toString());
			ja.add(jo);
		}
		return ja;
	}

	public void saveUserAuthority(UserAuthority userAuthority) throws Exception {
		userAuthorityDao.saveUserAuthority(userAuthority);
	}

	public void deleteUserAuthority(UserAuthority userAuthority) throws Exception {
		userAuthorityDao.deleteUserAuthority(userAuthority);
	}
	
	public UserAuthority findById(int uaid) throws Exception {
		// TODO Auto-generated method stub
		return userAuthorityDao.findUserAuthorityById(uaid);
	}

	public List<Authority> getAuthorityByUid(int uid) throws Exception {
		// TODO Auto-generated method stub
		return userAuthorityDao.getAuthorityByUid(uid);
	}

}
